<?php
/**
 *权限调用类
 *@author sulwan@126.com
 */

 class Catalog extends Application{
    public $Classlist;

 public function __construct() {
    parent::__construct();
 }



/**
 *分类数据赋值
 */
public function catalogclassSort() {
     $rsList = $this->catalogModulegory();
     $this->Classlist = new CCategory();
     $this->Classlist->assign ($rsList, 'id', 'pid', 'sortrank');
}


public function getArctnewCount($findkey='') {
	$where = is_array($findkey) ? implode(' AND ', $findkey) : $findkey;
	$where = empty ($where) ? '' : ' WHERE ' . $where;
	$sql = 'SELECT count(*) FROM #happy_arctnew';
	$sql .= $where;
	return $this->_db->getOne($sql);
}




 function getArctneList($page,$num,$findkey='',$order='id',$fields='*',$orderby='-') {
	$numrows = intval($num);
	$page = (intval($page) > 0) ? intval($page) : 1;
	$offset = ($page -1) * $numrows;
	#
	$where = is_array($findkey) ? implode(' AND ', $findkey) : $findkey;
	$where = empty ($where) ? '' : ' WHERE ' . $where;
	#
	$orderby = ($orderby == '+') ? 'ASC' : 'DESC';
	$order = empty ($order) ? '' : (' ORDER BY ' . $order . ' ' . $orderby);
	#
	$limits =  '  LIMIT '.$offset.','.$numrows;
	#
	$sql = 'SELECT '. $fields . ' FROM ' . '#happy_arctnew';
	$sql .= $where . $order . $limits;
	#
	return $this->_db->getAll($sql);
}

/**
 *添加文章
 */
public function saveAddNew($data) {
	$data = array_map('mysql_escape',$data);
	$sql = "INSERT INTO `#happy_arctnew`(`".implode("`,`",array_keys($data))."`)VALUES('".implode("','",array_values($data))."')";
	$id = $this->_db->execute($sql);
	return $id;
}


/**
 *查看有哪些分类
 */
public function catalogTreeSort(){
	$data = getCache(CATALOG,'catalogTree');
	if(empty($data)) {
		$data = array();
		$s = $this->catalogclassSort();
		$temp = $this->Classlist->getTreeList();
		if(empty($temp[0]->id)) {
			return $data;
		}
		foreach ($temp as $k => $v) {					//添加深度
			$data[] = $v->obj;
			$data[$k]['depth'] = $v->depth;
			$fields[] = 'pid = '.$data[$k]['id'];
			$data[$k]['Children'] = $this->seachFields($fields);
			unset($fields);
		}
	}
		setCache(CATALOG,'catalogTree',$data);
        return $data;
}


/**
 *检索是否存存在指定字段的指定值
 */
public function seachFields($fields) {
	$where = is_array($fields) ? implode(' AND ', $fields) : $fields;
	$where = empty ($where) ? '' : ' WHERE ' . $where;
    $sql = "SELECT * FROM #happy_arctype";
	$sql .= $where;
    $data = $this->_db->getOne($sql);
	if(!empty($data)) {
		return TRUE;
	}
	return FALSE;
}


/**
*获取现有模块
*/
public function catalogModulegory() {
	$sql = "SELECT * FROM #happy_arctype;";
	$rs = $this->_db->getAll($sql);
    return $rs;
}


//根据用户组ID查询用户具有的编辑权限
//查询组编辑权限
public function seachUserEditRole($gid) {
	if(empty($gid)) {
		return 0;
	}
	$roleSql = "SELECT * FROM `#happy_edit_sys` WHERE gid = ".$gid." AND app = 1";
	$role =  $this->_db->getRow($roleSql);
	if(!empty($role['role'])) {
		$role['role'] = explode("|", $role['role']);
	}else{
		$role['role'] = array();
	}
	return $role;
}


//获取子分类
 function getCatalogSon($id) {
	 $sql = "SELECT *  FROM `#happy_arctype` WHERE `pid` = ".intval($id);
	 $catalogArray = $this->_db->getAll($sql);
	 return $catalogArray;
 }


//获取分类
function getCatalog($id){
	$sql = "select * from `#happy_arctype` where `id` = ".intval($id);
	$CatelogArray = $this->_db->getRow($sql);
	return $CatelogArray;
}
/**
 *添加模块
 */
 public function addRoleModule($data) {
	 	$data = array_map('mysql_escape',$data);
		$sql = "INSERT INTO `#happy_arctype`(`".implode("`,`",array_keys($data))."`)VALUES('".implode("','",array_values($data))."')";
		$data = $this->_db->execute($sql);
		delCache(CATALOG);
		return $data;
 }


/**
 *所属模块:根据父节点获取全部子节点的全部信息
 */
function Rolefzid ($id) {
    $this->roleclassSort();
    $rs				=	$this->Classlist->getChildren($id);
    if (!empty($rs)){
        foreach($rs as $k=>$v)
        {
          $temp[$k]['name']				=	$v->obj['text'];
          $temp[$k]['mid']				=	$v->obj['mid'];
          $temp[$k]['id']				=	$v->id;
		  $temp[$k]['pid']				=	$v->obj['pid'];
		  $temp[$k]['binid']				=	$v->obj['binid'];
        }
    }
    return $temp;
}
/**
 *模块数据赋值
 */
public function roleclassSort() {
     $rsList = $this->catalogModulegory();
     $this->Classlist = new CCategory();
     $this->Classlist->assign ($rsList, 'id', 'pid', 'taxis');
}


 /**
  *删除分类
  */
  public function DelteClass($id) {
	  $sql = "DELETE FROM `#happy_arctype` WHERE `id` = ".$id." LIMIT 1";
	  $sign = $this->_db->execute($sql);
	  delCache(CATALOG);
	  return $sign;
  }

/**
 *保存修改角色文章编辑权限
 */
 public function sys_SaveEditCatalog($data,$gid) {
		$data = array_map('mysql_escape',$data);
		foreach ($data as $key => $value){
			  $sqlBody .= '`'.$key.'`'." = '".$value."',";
		}
		$sqlBody = substr($sqlBody,0,-1);
		$sql = "UPDATE `#happy_edit_sys` SET ".$sqlBody." where   gid =".$gid." LIMIT 1";
		$data = $this->_db->execute($sql);
		delCache(CATALOG,$gid);
		return $data;
 }
  /***
   *删除新闻
   */
   public function DelteNew($id) {
	   $sql = "DELETE FROM #happy_arctnew WHERE id = ".$id." LIMIT 1";
	   return $this->_db->execute($sql);
   }


/**
 *查看模块
 */
 public function lookRoleModule($id) {
		$sql = "SELECT * FROM `#happy_arctype` WHERE id=".intval($id);
		return $this->_db->getRow($sql);
 }

 /**
  *获取新闻
  */
  public function getNew($id) {
	  $sql = "SELECT * FROM #happy_arctnew WHERE id=".$id;
	  return $this->_db->getRow($sql);
  }

 /**
  *获取新闻
  */
  public function lookNewMid($mid) {
	  $sql = "SELECT * FROM #happy_arctnew WHERE mid='".$mid."'";
	  return $this->_db->getRow($sql);
  }


/**
 *保存修改分类
 */
 public function editSaveModule($data,$id) {
		if(empty($id)) {
			return 0;
		}
		$data = array_map('mysql_escape',$data);
		foreach ($data as $key => $value){
			  $sqlBody .= $key." = '".$value."',";
		}
		$sqlBody = substr($sqlBody,0,-1);
		$sql = "UPDATE `#happy_arctype` SET ".$sqlBody." where   id =".$id." LIMIT 1";
		$data = $this->_db->execute($sql);
        delCache(CATALOG);
 }

public function saveEditNew($data,$id) {
		if(empty($id)) {
			return 0;
		}
		$data = array_map('mysql_escape',$data);
		foreach ($data as $key => $value){
			  $sqlBody .= $key." = '".$value."',";
		}
		$sqlBody = substr($sqlBody,0,-1);
		$sql = "UPDATE `#happy_arctnew` SET ".$sqlBody." where   id =".$id." LIMIT 1";
		print_r($sql);exit;
		$data = $this->_db->execute($sql);
}

/**
 *设置分类排序
 */
public function taxisModulegory($value,$key) {
	if(empty($key)) {
		return 1;
	}
	$sql = " UPDATE `#happy_arctype` SET `sortrank` = '".intval($value)."' WHERE `id` =".$key;
    delCache(CATALOG);
	return $this->_db->execute($sql);
}

}
 ?>